// 添加路由记录
function addRouteRecord(route, pathMap, parentRecord) {
  // 拼接路由路径 /、/about、/about/a
  const path = parentRecord ? `${parentRecord.path === "/" ? "" : parentRecord.path}/${route.path}` : route.path;

  // 当前的路由记录
  const record = {
    path,
    component: route.component,
    props: route.props,
    meta: route.meta,
    parent: parentRecord
  };

  if (!pathMap[path]) {
    // 往pathMap 上添加每一条路由的映射
    pathMap[path] = record;
  }

  // 递归子路由
  route.children && route.children.forEach(childrenRoute => {
    addRouteRecord(childrenRoute, pathMap, record);
  })
}

// 创建路由映射表
export default function createRouteMap(routes, pathMap = {}) {
  // 拿到用户配置的路由表，转化成符合要求的映射表
  routes.forEach(route => {
    addRouteRecord(route, pathMap)
  })

  return {
    pathMap
  }
}

